<?php 
class IndexController extends Controller{
//	许愿内容存放数据库
	private $data;
	private $path;
//	登录内容存放数据库
	private $logindata;
	private $loginpath;
	public function __construct(){
//		许愿内容数据库载入
		$this->path="./data.php";
		$this->data=include $this->path;
//		许愿登录数据库载入
		$this->loginpath="./logindata.php";
		$this->logindata=include $this->loginpath;
	} 
	
	/*
	 * 默认访问index
	 */
	public function index(){
//		$d='Demo';		
//		$this->assign('d',$d);
		$this->assign('data',$this->data);
		$this->display();
		
	}
//	许愿添加
	public function add(){
		$_POST['time']=date('Y-m-d H:i:s');
		
//		文本中的内容处理
		$_POST['title']=$this->dealHTML($_POST['title']);
		$_POST['content']=$this->dealHTML($_POST['content']);
		$_POST['user']=$_SESSION['uname'];
		$this->data[]=$_POST;
//		写入数据库
		$this->dataToFile($this->path,$this->data);
		$_POST['key']=count($this->data);
//		把php的数组变成json类型，反给js
		echo json_encode($_POST);
	}
//	许愿登录
	public function login(){
		foreach ($this->logindata as $user) {
//			实体处理和密码加密
			if($user['user']==$this->dealHTML($_POST['user'])&&$user['pwd']==md5($_POST['pwd'])){
				$_SESSION['uname'] = $_POST['user'];				
				echo 1;
				die;
			}
		}
		echo 0;	
	}
//	许愿注册
	public function register(){
		$_POST['user']=$this->dealHTML($_POST['user']);
		foreach ($this->logindata as $user) {
			if($user['user']==$_POST['user']){
				echo 0;
				die;
			}
		}
		$this->logindata[]=array(
			'user'=>$_POST['user'],
			'pwd'=>md5($_POST['pwd'])
		);
		$this->dataToFile($this->loginpath,$this->logindata);
		echo 1;
	}
	
	
	/**
	 * 退出
	 */
	public function out(){
		//删除session变量
		session_unset();
		//删除session文件
		session_destroy();
		$this->success('退出成功','index.php');
	}

}









 ?>